package com.example.houduan.Controller;

import com.example.houduan.Pojo.Drug;
import com.example.houduan.Service.DrugService;
import com.example.houduan.Utils.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Controller
@CrossOrigin
public class DrugController {

    @Autowired
    DrugService drugService;

    @RequestMapping("/getDrugPage")
    @ResponseBody
    public PageModel getDrugPage(@RequestParam(defaultValue ="1" ) int num,
                                 @RequestParam(defaultValue = "3") int size){
//        System.out.println(drugService.getDrugPage(num,size));
        return drugService.getDrugPage(num,size);
    }

    @RequestMapping("/addDrug")
    @ResponseBody
    public String addDrug(@RequestBody Drug drug){
        drug.setCreatetime(LocalDateTime.now());
        drug.setUpdatetime(LocalDateTime.now());
        if(drugService.addDrug(drug)==0){
            return "添加失败";
        }else{
            return "添加成功";
        }
    }

    @RequestMapping("deleteDrug")
    @ResponseBody
    public String deleteDrug(Integer drug_id){
        if(drugService.deleteDrug(drug_id)==0){
            return "删除失败";
        }else{
            return "删除成功";
        }
    }

    @RequestMapping("updateDrug")
    @ResponseBody
    public String updateDrug(@RequestBody Drug drug){
        System.out.println(drug);
        System.out.println(drug.getDrugCompany());
        System.out.println(drug.getDrugCompany() != null ? drug.getDrugCompany().getCompany_id() : "null");
        drug.setUpdatetime(LocalDateTime.now());
        if (drugService.updateDrug(drug)==0){
            return "修改失败";
        }else{
            return "修改成功";
        }
    }

    @RequestMapping("/getDrugByConditions")
    @ResponseBody
    public List<Drug> getDrugByConditions(@RequestParam(required = false) Integer drug_id,String drug_name,String company_name){
        return drugService.getDrugByConditions(drug_id,drug_name,company_name);
    }

    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            return "文件为空";
        }
        
        String originalFilename = file.getOriginalFilename();
        String fileExtension = "";
        if (originalFilename != null && originalFilename.contains(".")) {
            fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        
        String uniqueFileName = UUID.randomUUID().toString() + fileExtension;
        String uploadDir = "D:\\uploads\\drugs\\";
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        
        String filePath = uploadDir + uniqueFileName;
        File destFile = new File(filePath);
        
        try {
            file.transferTo(destFile);
            String imageUrl = "/uploads/drugs/" + uniqueFileName;
            return imageUrl;
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败: " + e.getMessage();
        }
    }

}
